<template>
  <a-modal
    title="编辑表单"
    width="40%"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="()=>{this.visible=false}"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form" :model="entity" :rules="rules" v-bind="layout">
        <a-form-item label="菜单名" prop="Name">
          <a-input v-model="entity.title" autocomplete="off" />
        </a-form-item>
        <a-form-item label="菜单别名" prop="Name">
          <a-input v-model="entity.name" autocomplete="off" />
        </a-form-item>
        <a-form-item label="级别" prop="Name">
          <a-input v-model="entity.level" type="number" autocomplete="off" />
        </a-form-item>
        <a-form-item label="上级菜单" prop="ParentId">
          <a-select v-model="entity.parentId" allowClear>
            <a-select-option v-for="item in parentMenu " :key="item.id">{{ item.title }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="图标" prop="Icon">
          <a-input v-model="entity.icon" autocomplete="off" />
        </a-form-item>
        <a-form-item label="排序" prop="Sort">
          <a-input v-model="entity.sort" type="number" autocomplete="off" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { getMenuList, createMenu, updateMenu } from '@/api/system'

export default {
  props: {
    afterSubmit: {
      type: Function,
      default: null
    }
  },
  components: {
  },
  data() {
    return {
      id:0,
      layout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 18 }
      },
      visible: false,
      confirmLoading: false,
      entity: {},
      ParentIdTreeData: [],
      rules: {
        Name: [{ required: true, message: '必填' }],
        Type: [{ required: true, message: '必填' }],
        NeedAction: [{ required: true, message: '必填' }]
      },
      parentMenu:[]
    }
  },
  created() {
  },
  methods: {
    getMenuList(){
      getMenuList({},0).then(res=>{
        this.parentMenu=res.data.list
      })
    },
    init(id) {
      this.getMenuList()
      this.visible = true
      this.entity = {}
      this.$nextTick(() => {
        // this.$refs.form.clearValidate()
      })
    },
    openForm(id) {
      this.id=id
      this.init(id)

      if (id) {
        this.$http.get('/menu/'+id, { }).then(res => {
          this.entity = res.data
        })
      }
    },
    handleSubmit() {
      this.confirmLoading = false
      if(this.id==0){
        createMenu(this.entity).then(res=>{
          if(res.code==200){
            this.$message.success('操作成功!')
            this.visible = false
            this.confirmLoading = false
            this.afterSubmit()
          }
          else{
            this.$message.success(res.msg)
            this.confirmLoading = false
          }
        })
      }
      else{
        updateMenu(this.entity,this.entity.id).then(res=>{
          if(res.code==200){
            this.$message.success('操作成功!')
            this.visible = false
            this.confirmLoading = false
            this.afterSubmit()
          }
          else{
            this.$message.success(res.msg)
            this.confirmLoading = false
          }
        })
      }
      // this.$refs.form.validate(valid => {
      //   if (!valid) {
      //     return
      //   }
      //   this.confirmLoading = true
      //   // this.entity.permissionList = this.$refs.permissionList.getPermissionList()
      //   console.log(this.entity)
      //   // this.$http.post('/Base_Manage/Base_Action/SaveData', this.entity).then(resJson => {
      //   //   this.confirmLoading = false

      //   //   if (resJson.Success) {
      //   //     this.$message.success('操作成功!')
      //   //     this.afterSubmit()
      //   //     this.visible = false
      //   //   } else {
      //   //     this.$message.error(resJson.Msg)
      //   //   }
      //   // })
      // })
    }
  }
}
</script>
<style>
.select{
  height: 300px;
}
</style>
